<template>
  <div class="main">
    <div class="login border-radius box-shadow">
      <div class="login-title">
        <div>{{ title }}</div>
        <div class="f-gray-color">欢迎回来，我们想念你！</div>
      </div>
      <div class="login-form">
        <!--登录表单-->
        <el-form :model="loginForm" ref="loginRef" :rules="loginRules" status-icon>
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入账号"/>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                type="password"
                v-model="loginForm.password"
                placeholder="请输入密码"
                show-password
            />
          </el-form-item>
          <el-form-item prop="code">
            <el-input v-model="loginForm.code" placeholder="请输入验证码" @keyup.enter="login(loginRef)">
              <template #append>
                <Image class="pointer" style="width: 130px; height: 32px" :src="codeUrl" @click="getCodeUrl"/>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="loginBtn" type="primary" @click="login(loginRef)">登录</el-button>
          </el-form-item>
          <el-form-item>
            <el-button class="loginBtn" type="success" @click="navigateTo('/')">返回首页</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang='ts'>
import {FormInstance, FormRules} from "element-plus";
import {LoginUser} from "@/entity/user";

const {ModalUtils} = useUtils();
const {captchaApi} = useApi();
const title = ref("登录");
const loginForm = reactive<LoginUser>({
  username: "",
  password: "",
  code: "",
  uuid: "",
});
const loginRef = ref<FormInstance>();
const loginRules = reactive<FormRules>({
  username: [
    {required: true, message: '请填写用户名', trigger: 'blur'},
    {min: 5, max: 20, message: '用户名长度在 5 到 20 个字符', trigger: 'blur'},
  ],
  password: [
    {required: true, message: '请填写密码', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur'},
  ],
  code: [
    {required: true, message: '请填写验证码', trigger: 'blur'},
  ]
});
const codeUrl = ref();
const user = useStores.user();
const {setting} = useStoreToRefs(useStores.setting());
const route = useRoute();
const {loginStatus} = useStores.user();

useHead({
  title: '登录' + setting.value?.seoSeparate + setting.value?.name,
  meta: [
    {name: 'description', content: setting.value?.seoDescription},
    {name: 'keywords', content: '登录,注册,' + setting.value?.seoKeywords.join(",")}
  ]
});

onBeforeMount(() => {
  if (loginStatus) {
    ModalUtils.msgWarning(decodeURI("您已登录，请勿重复登录"));
    navigateTo('/');
  }
  let message = route.query?.message as string;
  if (message) {
    ModalUtils.msgWarning(decodeURI(message));
  }
  getCodeUrl();
});

// 获取验证码
const getCodeUrl = () => {
  captchaApi.getCaptchaImage().then((res: any) => {
    loginForm.uuid = res.data.uuid;
    codeUrl.value = "data:image/gif;base64," + res.data.img;
  });
};
// 登录方法
const login = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid) => {
    if (valid) {
      user.login(loginForm);
    } else {
      ModalUtils.msgWarning("请填写登录信息");
    }
    getCodeUrl();
  })
};
</script>

<style scoped lang="scss">
.main {
  position: fixed;
  width: 100%;
  height: 100%;
  background: url("https://api.minio.zym88.cn/navigation/bg/wallhaven-zyxvqy_1650P.jpg") no-repeat fixed bottom center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;

  .login {
    padding: 15px;
    width: 300px;
    border-bottom: 1px solid #F0F0F0;
    background-color: #FFFFFF;
  }

  .login-title {
    padding: 30px 0;
    text-align: center;

    div:nth-child(1) {
      font-size: 22px;
      line-height: 30px;
    }

    div:nth-child(2) {
      font-size: 14px;
      line-height: 30px;
    }
  }

  .loginBtn {
    width: 100%;
  }
}

::v-deep(.el-input-group__append) {
  width: 100px;
  height: 32px;
  background-color: #FFFFFF;
  border: none;
  padding: 0;
}
</style>

